﻿using System;
using System.Collections;
using WHC.Security.Common;

using WHC.Security.Entity;
using WHC.Security.IDAL;
using System.Collections.Generic;

namespace WHC.Security.BLL
{
	public class User : BaseBLL<UserInfo>
	{
		private IUser userDal;

		public User() : base()
		{
			this.userDal = (IUser) base.baseDal;
		}

		public override bool Delete(string key)
		{
			List<SimpleUserInfo> adminSimpleUsers = new Role().GetAdminSimpleUsers();
			if (adminSimpleUsers.Count == 1)
			{
				SimpleUserInfo info = (SimpleUserInfo) adminSimpleUsers[0];
                if (Convert.ToInt32(key) == info.ID)
				{
					throw new MyException("管理员角色至少需要包含一个用户！");
				}
			}
			return baseDal.DeleteByKey(key);
		}

		internal void EnabledExpire(int userID)
		{
            UserInfo info = (UserInfo)this.FindByID(userID.ToString());
			if (info.IsExpire)
			{
				info.IsExpire = false;
				this.Update(info, info.ID.ToString());
			}
		}

		private void FillNames(int pID, int currentID, int level, ref ArrayList userNamesAndIDs)
		{
			List<UserInfo> list = this.Find("PID=" + pID);
			int iD = 0;
			foreach (UserInfo info in list)
			{
				iD = info.ID;
				if (iD != currentID)
				{
					userNamesAndIDs.Add(new string('-', level*4) + info.Name);
					userNamesAndIDs.Add(iD.ToString());
					this.FillNames(iD, currentID, level + 1, ref userNamesAndIDs);
				}
			}
		}

		public ArrayList GetCanSetParent(int userID)
		{
			ArrayList userNamesAndIDs = new ArrayList();
			this.FillNames(-1, userID, 0, ref userNamesAndIDs);
			return userNamesAndIDs;
		}

		public UserInfo GetParent(int parentID)
		{
			return this.userDal.GetParent(parentID);
		}

        public List<SimpleUserInfo> GetSimpleUsers()
		{
			return this.userDal.GetSimpleUsers();
		}

        public List<SimpleUserInfo> GetSimpleUsers(string userIds)
		{
			return this.userDal.GetSimpleUsers(userIds);
		}

        public List<SimpleUserInfo> GetSimpleUsersByOU(int ouID)
		{
			return this.userDal.GetSimpleUsersByOU(ouID);
		}

		public List<SimpleUserInfo> GetSimpleUsersByRole(int roleID)
		{
			return this.userDal.GetSimpleUsersByRole(roleID);
		}

		public UserInfo GetUserByName(string userName)
		{
			UserInfo info = null;
			if (userName.Length > 0)
			{
				info = this.userDal.FindSingle("Name='" + userName + "'");
			}
			return info;
		}

		public List<FunctionInfo> GetUserFunctions(string identity, string sessionID, string typeID)
		{
			string userName = this.GetUserName(identity, sessionID);
			UserInfo userByName = this.GetUserByName(userName);
            List<FunctionInfo> functionsByUser = null;
			if (userByName != null)
			{
				functionsByUser = new Function().GetFunctionsByUser(userByName.ID, typeID);
			}
			return functionsByUser;
		}

		public string GetUserName(string identity, string sessionID)
		{
			if ((sessionID == null) || (sessionID == string.Empty))
			{
				return "";
			}
			string text = Convert.ToString(Convert.ToChar(1));
			identity = EncryptHelper.UnEncryptStr(identity, sessionID);
			int length = identity.IndexOf(text);
			return identity.Substring(0, length);
		}

		public List<UserInfo> GetUsersByOU(int ouID)
		{
			return this.userDal.GetUsersByOU(ouID);
		}

        public List<UserInfo> GetUsersByRole(int roleID)
		{
			return this.userDal.GetUsersByRole(roleID);
		}

		public override bool  Insert(UserInfo obj)
        {
			UserInfo info = (UserInfo) obj;
			info.Password = EncryptHelper.ComputeHash(info.Password, info.Name.ToLower());
			return base.Insert(obj);
		}

		public bool ModifyPassword(string userName, string userPassword)
		{
			bool flag = false;
			UserInfo userByName = this.GetUserByName(userName);
			if (userByName != null)
			{
				userPassword = EncryptHelper.ComputeHash(userPassword, userName.ToLower());
                userByName.Password = userPassword;
                flag = userDal.Update(userByName, userByName.ID.ToString());
			}
			return flag;
		}

		public override bool  Update(UserInfo obj, string primaryKeyValue)
		{
			UserInfo info = (UserInfo) obj;
			if (info.Password.Length < 50)
			{
				info.Password = EncryptHelper.ComputeHash(info.Password, info.Name.ToLower());
			}
            return this.userDal.Update(info, primaryKeyValue);
		}

		public bool UserInRole(string userName, string roleName)
		{
			UserInfo userByName = this.GetUserByName(userName);
			Role role = new Role();
			foreach (RoleInfo info2 in role.GetRolesByUser(userByName.ID))
			{
				if (info2.Name == roleName)
				{
					return true;
				}
			}
			return false;
		}

		public string VerifyUser(string userName, string userPassword, string sessionID)
		{
			if ((sessionID == null) || (sessionID == string.Empty))
			{
				return "";
			}
			string text = "";
			UserInfo userByName = this.GetUserByName(userName);
			if ((userByName != null) && !userByName.IsExpire)
			{
				userPassword = EncryptHelper.ComputeHash(userPassword, userName.ToLower());
				if (userPassword == userByName.Password)
				{
					text = EncryptHelper.EncryptStr(userName + Convert.ToString(Convert.ToChar(1)) + userPassword, sessionID);
				}
			}
			return text;
		}
	}
}